package com.djk.controller;

import com.djk.service.MusicService;
import com.djk.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/music")
public class MusicController {
    @Autowired
    private MusicService musicService;

    @RequestMapping("/upload")
    public Result upload(String singer, @RequestParam("filename") MultipartFile file, HttpServletRequest request) throws IOException {
        return musicService.upload(singer, file, request);
    }

    @RequestMapping("/getMusic")
    public Result getMusic(@RequestParam("path") String file) throws IOException {
        log.info(file);
        file = URLDecoder.decode(file, "UTF-8");
        return musicService.getMusic(file);
    }

    @RequestMapping("/delete")
    public Result deleteMusicById(@RequestParam("id") Integer musicId,HttpSession session) {
        return musicService.deleteMusicById(musicId,session);
    }

    @RequestMapping("/findMusicById")
    public Result findMusicById(Integer id) {
        return musicService.findMusicById(id);
    }

    /**
     * 批量删除请求
     *
     * @param ids
     * @return
     */
    @RequestMapping("/deleteSelect")
    public Result deleteSelect(@RequestParam("ids[]") List<Integer> ids, HttpSession session) {
        System.out.println(ids);
        return musicService.deleteSelect(ids,session);
    }

    /**
     * 模糊查询
     */
    @RequestMapping("/findMusic")
    public Result findMusic(@RequestParam(required = false) String musicName) {
        return musicService.findMusic(musicName);
    }
}
